package com.bxd.baodan.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.item.HotTopicBean.DataBean.TopicBean_;
import com.bxd.baodan.item.TopicLikeBean;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.NetUtil;
import com.wx.goodview.GoodView;
import com.bxd.baodan.adapter.ViewHolder;

import java.lang.reflect.Type;
import java.util.List;

public class HotTopicActivity extends BaseListActivity<TopicBean_> {
private GoodView mGoodView;

    @Override
    protected int getActivityLayout() {
        return R.layout.pull_refresh_layout_no_divider;
    }

    @Override
    protected void preDone() {
setActionBarTitle("全部话题");
        mGoodView = new GoodView(this);
        autoRefresh();
    }

    @Override
    protected Type getTypeToken() {
      return new TypeToken<List<TopicBean_>>() {
        }.getType(); 
    }

    @Override
    protected int getItemLayout() {
        return R.layout.hot_topic_item;
    }

    @Override
    protected void setNetUtil() {
        netUtil = new NetUtil(this, JsonApi.TOPIC_LISTS);
    }

    @Override
    public void convert(final ViewHolder holder, final TopicBean_ item) {
        int[] replyIds = {R.id.reply1, R.id.reply2, R.id.reply3, R.id.reply4, R.id.reply5};

        for (int replyId : replyIds) {
            holder.getView(replyId).setVisibility(View.GONE);
        }

        for (int i = 0; i < item.replyUsers.size(); i++) {
            holder.getView(replyIds[i]).setVisibility(View.VISIBLE);
            GlideUtil.loadCircle(HotTopicActivity.this, item.replyUsers.get(i).avatar, ((ImageView) holder.getView(replyIds[i])));
        }
        ((TextView) holder.getView(R.id.title)).setText("     " + item.title);
        ((TextView) holder.getView(R.id.replyCount)).setText(item.replyCount + "人参与");


        holder.getView(R.id.main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{item.url + ""});
            }
        });

        int[] lyIds = {R.id.ly1, R.id.ly2};
        int[] avatarIds = {R.id.avatar1, R.id.avatar2};
        int[] nicknameIds = {R.id.nickname1, R.id.nickname2};
        int[] createTimeIds = {R.id.createTime1, R.id.createTime2};
        int[] contentIds = {R.id.content1, R.id.content2};
        final int[] likeCountIds = {R.id.likeCount1, R.id.likeCount2};
        int[] goodIds = {R.id.good1, R.id.good2};
        int[] goodLys = {R.id.good1_ly,R.id.good2_ly};
        int[] isPlannerIds = {R.id.isPlanner,R.id.isPlanner2};

        for (int contentId : contentIds) {
            holder.getView(contentId).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{item.url + ""});
                }
            });
        }

        for (int lyId : lyIds) {
            holder.getView(lyId).setVisibility(View.GONE);

        }

        for (int i = 0; i < item.reply.size(); i++) {
            holder.getView(lyIds[i]).setVisibility(View.VISIBLE);
            GlideUtil.loadCircle(HotTopicActivity.this, item.reply.get(i).avatar, ((ImageView) holder.getView(avatarIds[i])));
            holder.setText(nicknameIds[i], item.reply.get(i).nickname);
            holder.setText(createTimeIds[i], item.reply.get(i).createTime);
            holder.setText(likeCountIds[i], item.reply.get(i).likeCount + "");
            holder.setText(nicknameIds[i], item.reply.get(i).nickname);
            holder.setText(contentIds[i], item.reply.get(i).content);



            final ImageView iv = ((ImageView) holder.getView(goodIds[i]));
            final View goodLy =  holder.getView(goodLys[i]);
            iv.setImageResource(item.reply.get(i).islike.equals("0") ? R.drawable.zan_gray : R.drawable.zan_green);
            ((TextView) holder.getView(likeCountIds[i])).setTextColor(getResources().getColor(item.reply.get(i).islike.equals("0") ? R.color.text_gray : R.color.green_lab));
            holder.getView(isPlannerIds[i]).setVisibility(item.reply.get(i).isPlanner.equals("0")?View.GONE:View.VISIBLE);

            final TextView likeTv = ((TextView) holder.getView(likeCountIds[i]));
            final int finalI = i;
            iv.setClickable(false);
            goodLy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (LoginInfo.getInstance(App.getContext()).isLogin()) {
                        NetUtil netu = new NetUtil(HotTopicActivity.this, JsonApi.TOPIC_REPLYLIKE);
                        netu.setParams("topicId", item.id);
                        netu.setParams("replyId", item.reply.get(finalI).id);
                        netu.postRequest("", new NetUtil.RequestStringListener() {
                            @Override
                            public void onComplete(String result) {


                                TopicLikeBean bean = new Gson().fromJson(result, TopicLikeBean.class);
                                String action = bean.data.nowLikeStatus + "";


                                int num = Integer.parseInt(likeTv.getText().toString());


                                if (action.equals("1")) {

                                    iv.setImageResource(R.drawable.zan_green);
                                    ObjectAnimator animatorX = ObjectAnimator.ofFloat(iv, "scaleX", 1.0f, 1.5f, 1.0f);
                                    ObjectAnimator animatorY = ObjectAnimator.ofFloat(iv, "scaleY", 1.0f, 1.5f, 1.0f);
                                    AnimatorSet animatorSet = new AnimatorSet();
                                    animatorSet.play(animatorX).with(animatorY);
                                    animatorSet.setDuration(1000);
                                    animatorSet.setInterpolator(new BounceInterpolator());
                                    animatorSet.start();
                                    item.reply.get(finalI).islike = "1";
                                    mGoodView.setTextInfo("+1", Color.parseColor("#14BF5B"), 12);

                                    mGoodView.show(iv);
                                    item.reply.get(finalI).likeCount =(num + 1) ;
                                    likeTv.setText((num + 1) + "");
                                    likeTv.setTextColor(getResources().getColor(R.color.green_lab));

                                } else if (action.equals("0")) {
                                    iv.setImageResource(R.drawable.zan_gray);
                                    item.reply.get(finalI).islike = "0";
                                    num = num > 0 ? num - 1 : num;
                                    item.reply.get(finalI).likeCount = num;

                                    likeTv.setText(num + "");
                                    likeTv.setTextColor(getResources().getColor(R.color.text_gray));
                                }
                               // notifyDataSetChanged();
                            }


                        });
                    } else {
                        showToast("登录后才能点赞");
                    }

                }
            });

        }


    }

    @Override
    protected String getArrayStr(String json_data) {
        return "list";
    }
}
